<?php
	defined( '_JEXEC' ) or die( 'Restricted access' );
	jimport( 'joomla.application.component.model' );
	jimport( 'joomla.methods' );
	require_once(JPATH_COMPONENT.DS.'dominio'.DS.'DTOFactory.php');
	require_once(JPATH_COMPONENT.DS.'data'.DS.'implFusionTables'.DS.'FTDAOFactory.php');
	require_once(JPATH_COMPONENT.DS.'data'.DS.'implJoomla'.DS.'JDAOFactory.php');
	
	class ModelSocialRoutesPOI extends JModel
	{
		var $_pois = null;
		
		function __construct()
		{
			parent::__construct();
		}
		
		public function getRoutePOIs($id){
			$daoJPois=JDAOFactory::getInstance()->getDAOPoi();
			$dtoPOIs=$daoJPois->getRoutePOIs($id);
			
			$daoJPOISubCategory=JDAOFactory::getInstance()->getDAOPOISubCategory();
			$daoJPOICategory=JDAOFactory::getInstance()->getDAOPOICategory();
			foreach ($dtoPOIs as $dtoPOI) {
				$idPOISubCategory=$dtoPOI->getSubCategory();
				$dtoPOISubCategory=$daoJPOISubCategory->getPOISubCategory($idPOISubCategory);
				$idPOICategory=$dtoPOISubCategory->getCategory();
				$dtoPOICategory=$daoJPOICategory->getPOICategory($idPOICategory);
				$dtoPOISubCategory->setCategory($dtoPOICategory);
				$dtoPOI->setSubCategory($dtoPOISubCategory);
			}
			$this->_pois=$dtoPOIs;
			return $this->_pois;
		}
		
		public function editPOI(){
			$daoFTPois=FTDAOFactory::getInstance()->getDAOPoi();
			$daoJPois=JDAOFactory::getInstance()->getDAOPoi();
			$dtoPOI=DTOFactory::getInstance()->getDTOPOIFromRequest();
			
			if($daoJPois->updatePOI($dtoPOI)){
				return $daoFTPois->updatePOI($dtoPOI);
			}
			return false;	
		}
		
		public function deletePOI(){
			$id=JRequest::getVar('id', '0');
			$daoFTPois=FTDAOFactory::getInstance()->getDAOPoi();
			$daoJPois=JDAOFactory::getInstance()->getDAOPoi();
			
			if($daoJPois->deletePOI($id)){
				return $daoFTPois->deletePOI($id);
			}
			return false;	
		}
		
		public function addPOI(){
			$daoFTPois=FTDAOFactory::getInstance()->getDAOPoi();
			$daoJPois=JDAOFactory::getInstance()->getDAOPoi();
			$dtoPOI=DTOFactory::getInstance()->getDTOPOIFromRequest();
			
			$id=$daoJPois->savePOI($dtoPOI);
			if($id){
				$dtoPOI->setId($id);
				$daoFTPois->savePOI($dtoPOI);
				return $dtoPOI->getId();
			}
			return false;
		}
		
		public function getPOICategories(){
			$daoPOICategory=JDAOFactory::getInstance()->getDAOPOICategory();
			$poiCategories=$daoPOICategory->getPOICategories();

			$daoPOISubCategory=JDAOFactory::getInstance()->getDAOPOISubCategory();
			foreach($poiCategories as $poiCategory){
				$poiSubCategories=$daoPOISubCategory->getPOICatSubCats($poiCategory->getId());
				foreach ($poiSubCategories as $poiSubCategory) {
					$poiSubCategory->setCategory($poiCategory);
				}
				$poiCategory->setSubCategories($poiSubCategories);
			}
			return $poiCategories;
		}
	}
?>